<?php

/*
 * ************************************************* 
 * Created on :2012-5-2 22:36:16
 * Encoding   :UTF-8
 * Description:
 * 
 * @Author @lqs <soso2501@gmail.com>
 * @WAP_WEIBO (C)1996-2099 SINA Inc. 
 * ************************************************
 */

class admin extends controller {

    function init() {
        if ($this->user->state != 0 || $this->user->role == "USER")
            die("没有权限");
        $this->tpl->assign("title", "河北工业大学校友会管理后台");
    }

    function index() {
        $this->tpl->assign("current_page", 'index');
        $r = $this->tpl->fetch("admin/header.html");
        $r.=$this->tpl->fetch("admin/footer.html");
        echo $r;
    }

    /**
     * 新闻管理的预执行 
     */
    function _n() {
        $this->tpl->assign("show_types", News::$allow_type);
        $this->tpl->assign("current_page", 'news');
        $this->tpl->assign("title", "新闻管理-河北工业大学校友会管理后台");
    }

    function editnews() {
        $this->tpl->assign("subtitle", "编辑");
        $this->_n();
        $newsid = empty($_GET['newsid']) ? '' : intval($_GET['newsid']);
        if (!empty($_POST)) {
            $r = News::modify($newsid, $_POST);
            if (!$r) {
                $msg = '<font style="color:red">修改失败</font>';
                $this->tpl->assign("msg", $msg);
            } else {
                $this->tpl->assign("msg", '<font style="color:green">修改成功</font>');
            }
        }
        $r = News::byid($newsid);
        if ($r) {
            $this->tpl->assign("js_set", json_encode($r));
            $r['u'] = $this->user->byid($r['userid']);
            $this->tpl->assign("news", $r);
            $this->tpl->assign("is_edit", true);
            $this->tpl->display("admin/addnews.html");
        } else {
            $this->addnews();
        }
    }

    function addnews() {
        $this->_n();
        $this->tpl->assign("subtitle", "新建");
        $this->tpl->assign("is_edit", false);
        if (!empty($_POST)) {
            $r = News::add($_POST['newstype'], $_POST['title'], $_POST['content'], $_POST['remark']);
            if ($r) {
                header("Location:/admin/editnews?newsid=$r");
            } else {
                $msg = '<font style="color:red">创建失败，请填写完整信息</font>';
                $this->tpl->assign("msg", $msg);
                $this->tpl->assign("js_set", json_encode($_POST));
                $this->tpl->display("admin/addnews.html");
            }
        } else {
            $this->tpl->display("admin/addnews.html");
        }
    }

    function news() {
        $this->_n();
        $type = empty($_REQUEST['type']) ? '' :
                (in_array($_REQUEST['type'], News::$allow_type) ? $_REQUEST['type'] : '');
        $key = empty($_REQUEST['key']) ? '' : $_REQUEST['key'];
        $condition = '';
        if (!empty($type))
            $condition .= "newstype='$type' and ";
        if (!empty($key))
            $condition .= "(title like '%$key%' or content like '%$key%' or remark like '%$key%') and ";
        $condition .= "1";
        //DB::debug();
        $n = News::countbycondition($condition);
        list($paging, $offset) = Toolfunc::paging($n);
        $this->tpl->assign("paging", $paging);
        $newses = News::bycondition($condition, $offset, PAGE_SIZE);
        Toolfunc::addnewslink($newses);
        $this->tpl->assign("news", $newses);
        $this->tpl->assign("js_set", json_encode(array("type" => $type, "key" => $key)));
        $this->tpl->display("admin/news.html");
    }

    /**
     * 相册管理 
     */
    function _a() {
        $this->tpl->assign("show_projects", Photo::$allow_project);
        $this->tpl->assign("current_page", 'album');
        $this->tpl->assign("title", "相册管理-河北工业大学校友会管理后台");
    }

    function album() {
        $this->_a();
        $condition = '';
        if (!empty($_REQUEST['project'])) {
            $condition .= "project = '{$_REQUEST['project']}' and ";
        }
        if (!empty($_REQUEST['key'])) {
            $condition .= "(name like '%{$_REQUEST['key']}%' or `describe` like '%{$_REQUEST['key']}%' or remark  like '%{$_REQUEST['key']}%') and ";
        }
        $condition .= "1";
        //DB::debug();
        $n = Photo::countalbumbycondition($condition);
        list($paging, $offset) = Toolfunc::paging($n);
        $this->tpl->assign("paging", $paging);
        $albums = Photo::albumbycondition($condition, $offset, PAGE_SIZE);
        $this->tpl->assign("albums", $albums);
        $this->tpl->assign("js_set", json_encode($_REQUEST));
        $this->tpl->display("admin/album.html");
    }

    function addalbum() {
        $this->_a();
        $this->tpl->assign("subtitle", "新建");
        $this->tpl->assign("is_edit", false);
        if (!empty($_POST)) {
            $cover = '';
            if (!empty($_FILES['cover']['name']) && $_FILES['cover']['error'] == 0) {
                $ext = pathinfo($_FILES['cover']['name'], PATHINFO_EXTENSION);
                $cover = Photo::upload($_FILES['cover']['tmp_name'], $ext);
            }
            $r = Photo::addalbum($_POST['name'], $_POST['describe'], $_POST['project'], "vcr", $_POST['remark'], $cover);
            if ($r) {
                header("Location:/admin/editalbum?albumid=$r");
            } else {
                $msg = '<font style="color:red">创建失败，请填写完整信息</font>';
                $this->tpl->assign("msg", $msg);
                $this->tpl->assign("js_set", json_encode($_POST));
                $this->tpl->display("admin/addalbum.html");
            }
        } else {
            $this->tpl->display("admin/addalbum.html");
        }
    }

    function editalbum() {
        $this->tpl->assign("subtitle", "编辑");
        $this->_a();
        $albumid = empty($_GET['albumid']) ? '' : intval($_GET['albumid']);
        if (!empty($_POST)) {
            $cover = '';
            if (!empty($_FILES['cover']['name']) && $_FILES['cover']['error'] == 0) {
                $ext = pathinfo($_FILES['cover']['name'], PATHINFO_EXTENSION);
                $cover = Photo::upload($_FILES['cover']['tmp_name'], $ext);
            }
            if (!empty($cover)) {
                $_POST['cover'] = $cover;
            } else {
                unset($_POST['cover']);
            }
            $r = Photo::modalbum($albumid, $_POST);
            if (!$r) {
                $msg = '<font style="color:red">修改失败</font>';
                $this->tpl->assign("msg", $msg);
            } else {
                $this->tpl->assign("msg", '<font style="color:green">修改成功</font>');
            }
        }
        $r = Photo::albumbyid($albumid);
        if ($r) {
            $this->tpl->assign("js_set", json_encode($r));
            $r['u'] = $this->user->byid($r['userid']);
            $this->tpl->assign("news", $r);
            $this->tpl->assign("album", $r);
            $this->tpl->assign("is_edit", true);
            $this->tpl->display("admin/addalbum.html");
        } else {
            $this->addalbum();
        }
    }

    function photo() {
        $this->_a();
        $albumid = empty($_REQUEST['albumid']) ? '' : intval($_REQUEST['albumid']);
        $album = Photo::albumbyid($albumid);
        if (!$album) {
            header("location:/admin/album");
            return;
        }
        $this->tpl->assign("album", $album);
        //DB::Debug();
        $n = Photo::countphotobyalbumid($albumid);
        list($paging, $offset) = Toolfunc::paging($n);
        $this->tpl->assign("paging", $paging);
        $photos = Photo::photobyalbumid($albumid, $offset, PAGE_SIZE);
        $this->tpl->assign("photos", $photos);
        $this->tpl->display("admin/photo.html");
    }

    function modphoto() {
        Photo::modphoto($_REQUEST['photoid'], array("describe" => $_REQUEST['describe']));
    }

    function copy() {
        $this->tpl->assign("current_page", 'copy');
        $this->tpl->assign("title", "文案管理-河北工业大学校友会管理后台");
        //db::debug();
        if (!empty($_POST)) {
            Copy::mod($_POST['copytitle'], $_POST['content'], $_POST['remark']);
            $this->tpl->assign("c_title", $_POST['copytitle']);
        }else
            $this->tpl->assign("c_title", "");
        $this->tpl->assign("alltitle", Copy::alltitles());
        $this->tpl->display("admin/copy.html");
    }

    function copyforone() {
        $r = Copy::get(empty($_REQUEST['title']) ? "" : $_REQUEST['title']);
        die(json_encode($r));
    }

    function place() {
        $this->tpl->assign("current_page", 'place');
        $this->tpl->assign("title", "分会管理-河北工业大学校友会管理后台");
        //db::debug();
        if (!empty($_POST)) {
            Place::mod($_POST['name'], $_POST['describe']);
            $this->tpl->assign("c_name", $_POST['name']);
        }else
            $this->tpl->assign("c_name", "");
        $this->tpl->assign("allnames", Place::allnames());
        $this->tpl->display("admin/place.html");
    }

    function placeforone() {
        //DB::Debug();
        $r = Place::get(empty($_REQUEST['name']) ? "" : $_REQUEST['name']);
        die(json_encode($r));
    }

    function user() {
        $this->tpl->assign("current_page", 'user');
        $this->tpl->assign("title", "用户管理-河北工业大学校友会管理后台");
        $this->tpl->assign("show_roles", USER::$allow_roles);
        $role = empty($_REQUEST['role']) ? '' :
                (in_array($_REQUEST['role'], array_keys(USER::$allow_roles)) ? $_REQUEST['role'] : '');
        $pass = empty($_REQUEST['pass']) ? "" :
                (in_array($_REQUEST['pass'], array("Y", "N")) ? $_REQUEST['pass'] : "");
        $key = empty($_REQUEST['key']) ? '' : $_REQUEST['key'];
        $condition = '';
        if (!empty($role))
            $condition .= "role='$role' and ";
        if (!empty($pass))
            $condition .= "pass='$pass' and ";
        if (!empty($_REQUEST['key']))
            $condition .= "(username like '%$key%' or email like '%$key%' or realname like '%$key%' or phone like '%$key%' 
                or realname like '%$key%'  or workunit like '%$key%' or class like '%$key%' ) and ";
        if (!empty($_REQUEST['realname']))
            $condition .=" realname like '%{$_REQUEST['realname']}%' and ";
        if (!empty($_REQUEST['username']))
            $condition .=" username like '%{$_REQUEST['username']}%' and ";
        if (!empty($_REQUEST['iyear']))
            $condition .=" iyear='$iyear' and ";
        $condition .= "1";
        //DB::debug();
        $n = $this->user->countbycondition($condition);
        list($paging, $offset) = Toolfunc::paging($n);
        $this->tpl->assign("paging", $paging);
        $users = $this->user->bycondition($condition, $offset, PAGE_SIZE);
        $this->tpl->assign("users", $users);
        $this->tpl->assign("js_set", json_encode($_REQUEST));
        $this->tpl->display("admin/user.html");
    }

    public function userpass() {
        $this->user->pass($_REQUEST['userid']);
    }

}

/* End of file admin */


